// This is unpublished source code. Michah Lerner 2006

package trieMatch.keywordMatcher;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class sequencer builds a lexically ordered sequence of {@link trieMatch.keywordMatch.StructuredElement} objects.
 * Tokens are sequences of unicode letters, digits and the underscore. The original order is recoverable with the
 * 
 * 
 * @author Michah.Lerner
 */
class Sequencer {

	final static String _tokenPatternString = "[\\p{L}0-9_]+"; // "[a-zA-Z0-9_]+"; // "[\\S]+";
	String tokenPatternString;
	Pattern tokenPattern;
	StructuredElement element;

	Sequencer() {
		this(_tokenPatternString);
	}

	Sequencer(final String tokenPatternString) {
		this.tokenPatternString = tokenPatternString;
		tokenPattern = Pattern.compile(tokenPatternString);
	}

	synchronized StructuredElement arrange(final String string) {
		final List<String> pieces = new ArrayList<String>();
		final Matcher m = tokenPattern.matcher(string);
		while (m.find()) {
			pieces.add(string.substring(m.start(), m.end()).toLowerCase(Locale.getDefault()));
		}
		element = StructuredElement.mkStructuredElement(pieces);
		element.arrangeAtoms();
		return element;
	}
}
